package com.fin.zw.intelligentbot.service;

import com.fin.zw.intelligentbot.entity.AiAccount;
import com.fin.zw.intelligentbot.entity.Company;
import com.fin.zw.intelligentbot.entity.QAiAccount;
import com.fin.zw.intelligentbot.enums.SourceType;
import com.fin.zw.intelligentbot.model.request.AiAccountRequest;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.repository.AiAccountRepository;
import com.querydsl.core.BooleanBuilder;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service("aiAccountService")
public class AiAccountService {

    private static final Logger log = LoggerFactory.getLogger(AiAccountService.class);

    @Autowired
    private AiAccountRepository aiAccountRepository;

    @Autowired
    private CommonService commonService;

    /**
     * 分页查询ai账号信息
     * @param pageable
     * @param sessionModel
     * @return
     */
    public Page<AiAccount> queryAiAccount(Pageable pageable,
                                          SessionModel sessionModel,
                                          String name){
        BooleanBuilder builder = new BooleanBuilder();
        QAiAccount qAiAccount = QAiAccount.aiAccount;
        if(StringUtils.isNoneBlank(name)){
            builder.and(qAiAccount.name.eq(name));
        }
        if(sessionModel.getCompanyId() == null){ //管理端
            builder.and(qAiAccount.source.eq(SourceType.SAFEMANAGER));
        }else{ //企业方
            Company company = commonService.getCompany(sessionModel);
            builder.and(qAiAccount.companies.contains(company));
        }
        return aiAccountRepository.findAll(builder,pageable);
    }

    /**
     * 查询ai账号信息
     * @param sessionModel
     * @return
     */
    public List<AiAccount> getAiAccountList(SessionModel sessionModel){
        BooleanBuilder builder = new BooleanBuilder();
        QAiAccount qAiAccount = QAiAccount.aiAccount;
        if(sessionModel.getCompanyId() == null){ //管理端
            builder.and(qAiAccount.source.eq(SourceType.SAFEMANAGER));
        }else{ //企业方
            Company company = commonService.getCompany(sessionModel);
            builder.and(qAiAccount.companies.contains(company));
        }
        Iterable<AiAccount> iterable = aiAccountRepository.findAll(builder);
        return IterableUtils.toList(iterable);
    }

    /**
     * ai账号的添加
     * @param request
     * @param sessionModel
     */
    public void createAiAccount(AiAccountRequest request,SessionModel sessionModel){
        AiAccount aiAccount = new AiAccount();
        Set<Company> set = commonService.getCompanySet(sessionModel,request.getCompanyId());
        BeanUtils.copyProperties(request,aiAccount);
        aiAccount.setCompanies(set);
        if(sessionModel.getCompanyId() == null){
            aiAccount.setSource(SourceType.SAFEMANAGER);
        }else{
            aiAccount.setSource(SourceType.COMPANY);
        }
        aiAccountRepository.save(aiAccount);
    }

    /**
     * ai账号的修改
     * @param request
     * @param sessionModel
     */
    public void updateAiAccount(AiAccountRequest request,SessionModel sessionModel){
        Optional<AiAccount> optional = aiAccountRepository.findById(request.getId());
        if(!optional.isPresent()){
            log.error("ai账号信息不存在");
        }
        AiAccount aiAccount = optional.get();
        aiAccount.setToken(request.getToken());
        aiAccount.setAccount(request.getAccount());
        aiAccount.setUrl(request.getUrl());
        Set<Company> set = commonService.getCompanySet(sessionModel,request.getCompanyId());
        aiAccount.setCompanies(set);
        aiAccount.setPwd(request.getPassword());
        aiAccount.setName(request.getName());
        aiAccount.setLastTokenTime(request.getLastTokenTime());
        aiAccountRepository.save(aiAccount);
    }
}
